package org.jeecg.modules.inPlantUpload.controller;

import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.util.CommonUtils;
import org.jeecg.modules.carInformationUpload.util.Base64ToMultipartFile;
import org.jeecg.modules.inPlantUpload.entity.InPlantUpload;
import org.jeecg.modules.inPlantUpload.service.IInPlantUploadService;
import org.jeecg.modules.networking.service.INetWorkingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.Date;

/**
 * @Description: 厂内运输车辆信息上传表
 * @Author: jeecg-boot
 * @Date:   2024-07-23
 * @Version: V1.0
 */
@Api(tags="厂内运输车辆信息上传表")
@RestController
@RequestMapping("/inPlantUpload/inPlantUpload")
@Slf4j
public class InPlantUploadController extends JeecgController<InPlantUpload, IInPlantUploadService> {
	@Value(value = "${jeecg.uploadType}")
	private String uploadType;
	@Autowired
	private IInPlantUploadService inPlantUploadService;
	@Autowired
	private INetWorkingService netWorkingService;
	
	/**
	 * 分页列表查询
	 *
	 * @param inPlantUpload
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@AutoLog(value = "厂内运输车辆信息上传表-分页列表查询")
	@ApiOperation(value="厂内运输车辆信息上传表-分页列表查询", notes="厂内运输车辆信息上传表-分页列表查询")
	@GetMapping(value = "/list")
	public Result<?> queryPageList(InPlantUpload inPlantUpload,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<InPlantUpload> queryWrapper = QueryGenerator.initQueryWrapper(inPlantUpload, req.getParameterMap());
		Page<InPlantUpload> page = new Page<InPlantUpload>(pageNo, pageSize);
		IPage<InPlantUpload> pageList = inPlantUploadService.page(page, queryWrapper);
		return Result.OK(pageList);
	}

	 @AutoLog(value = "厂内运输车辆信息上传表-对外-分页列表查询")
	 @ApiOperation(value="厂内运输车辆信息上传表-对外-分页列表查询", notes="厂内运输车辆信息上传表-对外-分页列表查询")
	 @GetMapping(value = "/m/list")
	 public Result<?> mQueryPageList(InPlantUpload inPlantUpload,
									@RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
									@RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
									HttpServletRequest req) {
		 QueryWrapper<InPlantUpload> queryWrapper = QueryGenerator.initQueryWrapper(inPlantUpload, req.getParameterMap());
		 queryWrapper.orderByDesc("create_time");
		 Page<InPlantUpload> page = new Page<InPlantUpload>(pageNo, pageSize);
		 IPage<InPlantUpload> pageList = inPlantUploadService.page(page, queryWrapper);
		 return Result.OK(pageList);
	 }
	
	/**
	 *   添加
	 *
	 * @param inPlantUpload
	 * @return
	 */
	@AutoLog(value = "厂内运输车辆信息上传表-添加")
	@ApiOperation(value="厂内运输车辆信息上传表-添加", notes="厂内运输车辆信息上传表-添加")
	@PostMapping(value = "/add")
	public Result<?> add(@RequestBody InPlantUpload inPlantUpload) {
		inPlantUploadService.save(inPlantUpload);
        try {
			JSONObject jsonObject = netWorkingService.putVehicleInfo(inPlantUpload);
			Integer code = jsonObject.getInteger("code");
			if (code == 200) {
				inPlantUpload.setUploadStatus("1");
			} else {
				log.warn("接口返回错误结果: {}", jsonObject);
			}
		} catch (Exception e) {
            log.error(e.getMessage());
        }
        return Result.OK("添加成功！");
	}

	 @AutoLog(value = "厂内运输车辆信息上传表-对外-添加")
	 @ApiOperation(value="厂内运输车辆信息上传表-对外-添加", notes="厂内运输车辆信息上传表-对外-添加")
	 @PostMapping(value = "/m/add")
	 public Result<?> mAdd(@RequestBody InPlantUpload inPlantUpload) {
		 String inventory = inPlantUpload.getInventory();
		 String license = inPlantUpload.getLicense();
		 MultipartFile inventoryFile = new Base64ToMultipartFile(inventory, "png", "image/png");
		 MultipartFile licenseFile = new Base64ToMultipartFile(license, "png", "image/png");
		 String bizPath = DateUtil.formatDate(new Date());
		 String inventoryPath = CommonUtils.upload(inventoryFile, bizPath, uploadType);
		 String licensePath = CommonUtils.upload(licenseFile, bizPath, uploadType);
		 inPlantUpload.setInventory(inventoryPath);
		 inPlantUpload.setLicense(licensePath);
		 inPlantUploadService.save(inPlantUpload);
		 try {
			 JSONObject jsonObject = netWorkingService.putVehicleInfo(inPlantUpload);
			 Integer code = jsonObject.getInteger("code");
			 if (code == 200) {
				 inPlantUpload.setUploadStatus("1");
			 } else {
				 log.warn("接口返回错误结果: {}", jsonObject);
			 }
		 } catch (Exception e) {
			 log.error(e.getMessage());
		 }
		 return Result.OK("添加成功！");
	 }
	
	/**
	 *  编辑
	 *
	 * @param inPlantUpload
	 * @return
	 */
	@AutoLog(value = "厂内运输车辆信息上传表-编辑")
	@ApiOperation(value="厂内运输车辆信息上传表-编辑", notes="厂内运输车辆信息上传表-编辑")
	@PutMapping(value = "/edit")
	public Result<?> edit(@RequestBody InPlantUpload inPlantUpload) {
		inPlantUploadService.updateById(inPlantUpload);
		return Result.OK("编辑成功!");
	}
	
	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "厂内运输车辆信息上传表-通过id删除")
	@ApiOperation(value="厂内运输车辆信息上传表-通过id删除", notes="厂内运输车辆信息上传表-通过id删除")
	@DeleteMapping(value = "/delete")
	public Result<?> delete(@RequestParam(name="id",required=true) String id) {
		inPlantUploadService.removeById(id);
		return Result.OK("删除成功!");
	}
	
	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "厂内运输车辆信息上传表-批量删除")
	@ApiOperation(value="厂内运输车辆信息上传表-批量删除", notes="厂内运输车辆信息上传表-批量删除")
	@DeleteMapping(value = "/deleteBatch")
	public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.inPlantUploadService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}
	
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "厂内运输车辆信息上传表-通过id查询")
	@ApiOperation(value="厂内运输车辆信息上传表-通过id查询", notes="厂内运输车辆信息上传表-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
		InPlantUpload inPlantUpload = inPlantUploadService.getById(id);
		if(inPlantUpload==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(inPlantUpload);
	}

	 @AutoLog(value = "厂内运输车辆信息上传表-通过环保登记编码查询")
	 @ApiOperation(value="厂内运输车辆信息上传表-通过环保登记编码查询", notes="厂内运输车辆信息上传表-通过环保登记编码查询")
	 @GetMapping(value = "/queryByRegistrationCode")
	 public Result<?> queryByRegistrationCode(@RequestParam(name="registrationCode",required=true) String registrationCode) {
		 InPlantUpload inPlantUpload = inPlantUploadService.getByRegistrationCode(registrationCode);
		 if(inPlantUpload==null) {
			 return Result.error("未找到对应数据");
		 }
		 return Result.OK(inPlantUpload);
	 }

    /**
    * 导出excel
    *
    * @param request
    * @param inPlantUpload
    */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, InPlantUpload inPlantUpload) {
        return super.exportXls(request, inPlantUpload, InPlantUpload.class, "厂内运输车辆信息上传表");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, InPlantUpload.class);
    }

}
